package linkLine

import (
	"fmt"
	"strconv"
)

// 作为首元节点，有无皆可
type SignleList struct {
	len  int
	node *SingleNode
}

func Create() *SignleList {
	return new(SignleList)
}

func (sl *SignleList) Append(e *SingleNode) {
	if sl.len > 0 {
		sl.node.SetNext(e)
	} else {
		sl.node = e
	}
	sl.len++
}

func (sl *SignleList) Len() int {
	return sl.len
}

func (sl *SignleList) IsEmpty() bool {
	return sl.len == 0
}

func (sl *SignleList) Get(i int) *SingleNode {
	if i < 0 || i > sl.len {
		panic("超出")
	}
	se := sl.node
	if i > 0 {
		for index := 0; index < i; index++ {
			se = se.Next
		}
	}
	return se
}

func (sl *SignleList) Insert(index int, e *SingleNode) {
	prior := sl.node
	i := 1
	for ; prior.Next != nil && i < index-1; i++ {

		prior = prior.Next
	}
	if prior == nil || i != index-1 {
		panic("未找到" + strconv.Itoa(index) + "上一个元素")
	}
	e.Next = prior.Next
	prior.Next = e
	sl.len++
}

func (sl *SignleList) Dd() {
	prior := sl.node
	for {
		fmt.Printf("%c \n", prior.Value)
		if prior.Next != nil {
			prior = prior.Next
		} else {
			break
		}
	}
}
